package cn.enjoy.aspect;

import cn.enjoy.annotation.Log;
import cn.enjoy.service.ISystemLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 具体切片类的实现
 *
 * @author K
 */
@Aspect
@Component
public class SystemLogAspect {

    /**
     * 注入Service用于把日志保存数据库
     */
    @Autowired
    private ISystemLogService systemLogService;

    private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

    @Pointcut("execution(* cn.enjoy.service..UserServiceImpl.findAll(..))")
    public void userServiceAspect() {
    }

    @Before("userServiceAspect()")
    public void beforeOperation(JoinPoint joinPoint) {
        System.out.println("==========执行controller前置通知===============");
        if (logger.isInfoEnabled()) {
            logger.info("before " + joinPoint);
        }
    }

    @Around("userServiceAspect()")
    public Object aroundOperation(JoinPoint joinPoint) {
        System.out.println("==========开始执行controller环绕通知===============");
        long start = System.currentTimeMillis();
        try {
            Object object = ((ProceedingJoinPoint) joinPoint).proceed();
            long end = System.currentTimeMillis();
            if (logger.isInfoEnabled()) {
                logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");
            }
            System.out.println("==========结束执行controller环绕通知===============");

            return object;
        } catch (Throwable e) {
            long end = System.currentTimeMillis();
            if (logger.isInfoEnabled()) {
                logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());
            }
        }

        return null;
    }

    @After("userServiceAspect()")
    public void afterOperation(JoinPoint joinPoint) throws ClassNotFoundException {
        System.out.println("==========执行controller后置通知===============");
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String operationType = "";
        String operationName = "";

        System.out.println(targetName);
        System.out.println(methodName);

        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                if (method.isAnnotationPresent(Log.class)) {
                    operationName = method.getAnnotation(Log.class).operationName();
                    operationType = method.getAnnotation(Log.class).operationType();
                }
            }
        }

        System.out.println(operationName);
        System.out.println(operationType);

        if (logger.isInfoEnabled()) {
            logger.info("after " + joinPoint);
        }
    }
}
